<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:nwisdocs="http://wales.nhs.uk/nwis/docs"
	exclude-result-prefixes="xs xsi">

  <xsl:template name="discharge" match="nwisdocs:DocumentBody/nwisdocs:HospitalDischarge">
    <div class="boxed-section" id="{local-name()}">
      <h2>Discharge details</h2>
      <xsl:choose>
        <xsl:when test="(nwisdocs:EffectiveTime/nwisdocs:Start/nwisdocs:Date[text() != '']) or
                  (nwisdocs:EffectiveTime/nwisdocs:Start/nwisdocs:Time[text() != '']) or
                  (nwisdocs:Method/nwisdocs:ClinicalInformation/nwisdocs:ClinicalCodeDescription[text() != '']) or
                  (nwisdocs:Destination/nwisdocs:Type/nwisdocs:ClinicalInformation/nwisdocs:ClinicalCodeDescription[text() != '']) or
                  (nwisdocs:Destination/nwisdocs:OtherAddress/nwisdocs:UnstructuredAddress[text() != '']) or
                  (nwisdocs:BackgroundData/nwisdocs:SocialCircumstances/nwisdocs:HousingDetail/nwisdocs:HouseholdComposition[text() != '']) or
                  (nwisdocs:ResponsibleConsultant/nwisdocs:Consultant/nwisdocs:HcpName/nwisdocs:UnstructuredName[text() != '']) or
                  (nwisdocs:ResponsibleConsultant/nwisdocs:Consultant/nwisdocs:HcpSpeciality/nwisdocs:SpecialityName[text() != '']) or
                  (nwisdocs:DischargedFromLocation/nwisdocs:Text[text() != ''])">

          <xsl:choose>
            <xsl:when test="nwisdocs:EffectiveTime/nwisdocs:Start/nwisdocs:Date[text() != '']">
              <label>Date of discharge</label>
              <span>
                <xsl:call-template name="standard_date">
                  <xsl:with-param name="date" select="nwisdocs:EffectiveTime/nwisdocs:Start/nwisdocs:Date"/>
                </xsl:call-template>
              </span>
              <br />
            </xsl:when>
          </xsl:choose>

          <xsl:choose>
            <xsl:when test="nwisdocs:EffectiveTime/nwisdocs:Start/nwisdocs:Time[text() != '']">
              <label>Time of discharge</label>
              <span>
                <xsl:call-template name="standard_time">
                  <xsl:with-param name="time" select="nwisdocs:EffectiveTime/nwisdocs:Start/nwisdocs:Time"/>
                </xsl:call-template>
              </span>
              <br/>
            </xsl:when>
          </xsl:choose>

          <xsl:choose>
            <xsl:when test="nwisdocs:Method/nwisdocs:ClinicalInformation/nwisdocs:ClinicalCodeDescription[text() != '']">
              <label>Method of discharge</label>
              <span>
                <xsl:value-of select="nwisdocs:Method/nwisdocs:ClinicalInformation/nwisdocs:ClinicalCodeDescription"/>
              </span>
              <br/>
            </xsl:when>
          </xsl:choose>

          <xsl:choose>
            <xsl:when test="nwisdocs:Destination/nwisdocs:Type/nwisdocs:ClinicalInformation/nwisdocs:ClinicalCodeDescription[text() != '']">
              <label>Type of destination</label>
              <span>
                <xsl:value-of select="nwisdocs:Destination/nwisdocs:Type/nwisdocs:ClinicalInformation/nwisdocs:ClinicalCodeDescription"/>
              </span>
              <br/>
            </xsl:when>
          </xsl:choose>

          <xsl:choose>
            <xsl:when test="nwisdocs:Destination/nwisdocs:OtherAddress/nwisdocs:UnstructuredAddress[text() != '']">
              <label>Destination address</label>
              <span>
                <xsl:value-of select="nwisdocs:Destination/nwisdocs:OtherAddress/nwisdocs:UnstructuredAddress"/>
              </span>
              <br/>
            </xsl:when>
          </xsl:choose>

          <xsl:choose>
            <xsl:when test="nwisdocs:BackgroundData/nwisdocs:SocialCircumstances/nwisdocs:HousingDetail/nwisdocs:HouseholdComposition[text() != '']">
              <label>Living alone</label>
              <span>
                <xsl:value-of select="nwisdocs:BackgroundData/nwisdocs:SocialCircumstances/nwisdocs:HousingDetail/nwisdocs:HouseholdComposition"/>
              </span>
              <br/>
            </xsl:when>
          </xsl:choose>

          <xsl:choose>
            <xsl:when test="nwisdocs:ResponsibleConsultant/nwisdocs:Consultant/nwisdocs:HcpName/nwisdocs:UnstructuredName[text() != '']">
              <label>Discharging consultant</label>
              <span>
                <xsl:value-of select="nwisdocs:ResponsibleConsultant/nwisdocs:Consultant/nwisdocs:HcpName/nwisdocs:UnstructuredName"/>
              </span>
              <br/>
            </xsl:when>
          </xsl:choose>

          <xsl:choose>
            <xsl:when test="nwisdocs:ResponsibleConsultant/nwisdocs:Consultant/nwisdocs:HcpSpeciality/nwisdocs:SpecialityName[text() != '']">
              <label>Speciality</label>
              <span>
                <xsl:value-of select="nwisdocs:ResponsibleConsultant/nwisdocs:Consultant/nwisdocs:HcpSpeciality/nwisdocs:SpecialityName"/>
              </span>
              <br/>
            </xsl:when>
          </xsl:choose>

          <xsl:choose>
            <xsl:when test="nwisdocs:DischargedFromLocation/nwisdocs:Text[text() != '']">
              <label>Ward</label>
              <span>
                <xsl:value-of select="nwisdocs:DischargedFromLocation/nwisdocs:Text"/>
              </span>
            </xsl:when>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <Label>None recorded</Label>
        </xsl:otherwise>
      </xsl:choose>
    </div>
  </xsl:template>

</xsl:stylesheet>
